<template>
	<view>
		<view class="category_tab">
			<view class="category_tab_title">
				<view class="title_inner">
					<uni-segmented-control
						:current="current" 
						:values="items.map(v=>v.title)" 
						@clickItem="onClickItem" 
						styleType="text" 
						activeColor="#d63f6e">
					</uni-segmented-control>
				</view>
				<view class="iconfont iconsearch"></view>
			</view>
			<scroll-view @scrolltolower="handleScrolltolower" enable-flex scroll-y  class="category_tab_content">
				<view class="cate_item"
				v-for="(item,index) in vertical"
				:key="item.id"
				>
					<image mode="withFix" :src="item.thumb"></image>	
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
import { uniSegmentedControl } from "@dcloudio/uni-ui"
export default{
	components:{
		uniSegmentedControl
	},
	data(){
		return{
			items:[{title:"最新",order:"new"},{title:"热门",order:"hot"}],
			current:0,
			params:{
				limit:30,
				skip:0,
				order:"new"
			},
			id:0,
			//页面显示的数组
			vertical:[],
			hasMore:true
		};
	},
	onLoad(options) {
		this.id=options.id;
		this.getList();
	},
	methods:{
		onClickItem(e){
			
			//1 根据被点击的标题 切换标题
			if(this.current !== e.currentIndex){
				this.current = e.currentIndex;
			}else{
				//点击的是相同的标题
				return;
			}
			// 2 切换order
			this.params.order=this.items[e.currentIndex].order;
			//不用再做分页
			this.params.skip=0;
			//数组要被清空掉
			this.vertical=[];
			// 3 重新发请求
			this.getList();
		},
		getList(){
			this.request({
				url:`http://service.picasso.adesk.com/v1/vertical/category/${this.id}/vertical`,
				data:this.params
			}).then(result=>{
				//判断还有没有数据
				if(result.res.vertical.length===0){
					this.hasMore=false;
					uni.showToast({
						title:"没有数据了",
						icon:"none"
					})
					return;
				}
				//有数据 vertical 要进行叠加
				this.vertical=[...this.vertical,...result.res.vertical];
			})
		},
		//下滑加载
		handleScrolltolower(){
			if(this.hasMore){
				this.params.skip+=this.params.limit;
				this.getList();
			}else{
				uni.showToast({
					title:"没有数据了",
					icon:"none"
				})
			}
		}
	}
}
</script>

<style lang="scss" scoped>
	.category_tab{
		.category_tab_title{
			position: relative;
			.title_inner{
				width: 60%;
				margin: 0 auto;
			}
			.iconsearch{
				position: absolute;
				top: 35%;
				transform: translate(-50%);
				right: 5%;
			}
		}
		.category_tab_content{
			display: flex;
			flex-wrap: wrap;
			height: calc(100vh - 36px);
			.cate_item{
				// display: inline-block; 
				width: 33.33%;
				border: 2px solid #ccc;
				image{
					
				}
			}
		}
	}
</style>
